import React from 'react';
import { inject, observer } from 'mobx-react';
import { getAntd, getEcCom, getLabel } from '../../../util/convert';
import trim from 'lodash/trim';
import {workflowPublicFiles} from "../../../../workflow/constants/fileConfig";
import * as asyncLoad from '../../../util/asyncLoad';


@inject('requestWithdrawStore', 'globalStore')
@observer
export default class Main extends React.Component {

    constructor(){
        super();
        this.state = {hasLoad: false};
    }
    componentDidMount(){
        const init = () =>{
            this.setState({hasLoad: true});
        }
        asyncLoad.asyncLoadModule('workflowPublic', workflowPublicFiles, init);
    }

    render() {
        const { hasLoad } = this.state;
        if(!hasLoad){
            return (<p/>);
        }
        const { globalStore, visible, requestWithdrawStore } = this.props;
        const { WfBatchSubmit } = window.weaWorkflowPublic;
        const { rightMenu = {}, commonParam = {} ,submitParam ={}} = globalStore;
        const { rightMenuNames = {} } = rightMenu;
        const { loading ,setState} = requestWithdrawStore;
        const onSubmitText = rightMenuNames['WF_WITHDRAW_0'] || getLabel(513318, '撤回');
        setState({'lastOperateDate':submitParam.lastOperateDate});
        setState({'lastOperateTime':submitParam.lastOperateTime});
        setState({'currentdate':submitParam.currentdate});
        setState({'currenttime':submitParam.currenttime});
        return (<WfBatchSubmit ecId={`${this && this.props && this.props.ecId || ''}_WfBatchSubmit@0m957p`} onSubmit={this.doSubmit} onCancel={this.doCancel} loading = {loading} visible={visible} onSubmitText={onSubmitText} iconClassName={'icon-coms-Revocation'} />);

    }
    doCancel = () => {
        const { requestWithdrawStore } = this.props;
        requestWithdrawStore.closeEvent();
    }

    getBottomBtns() {
        let btns = [
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@e6sunp@call`} type="primary" disabled={false} onClick={this.doSubmit} >{getLabel(513318, '撤回')}</Button>,
        ];
        return btns;
    }

    getMoreBtns() {
        let btns = {
            datas: [],
            collectParams: {
                favname: getLabel(513318, '撤回'),
                favouritetype: 2,
                objid: 0,
                importantlevel: 1,
            }
        };
        btns.datas.push(
            {
                key: '1',
                icon: <i className="icon-coms-Preservation" />,
                content: getLabel(513318, '撤回'),
                disabled: false,
                onClick: key => this.doSubmit(),
            }
        )
        return btns;
    }

    doSubmit = (remark = '') => {
        const { Button, Popover, message, Modal } = getAntd();
        if (this.chekcremark(remark)) {
            const { requestWithdrawStore } = this.props;
            const { allowRemind = 0, recallNodeName = "" } = requestWithdrawStore;
            let that = this;
            Modal.confirm({
                content: that.getConfirmContent(allowRemind, recallNodeName),
                okText: getLabel(83446, `确定`),
                cancelText: getLabel(31129, `取消`),
                onOk() {
                    requestWithdrawStore.doSubmit(remark);
                },
                onCancel() { },
            });
        }else{
            Modal.warning({
                content: (<p id = 'lqy' style = {{'textAlign':'center'}}>{getLabel(26013,"请填写签字意见！")}</p>),
                okText:getLabel(83446,"确定"),
                onOk() {
                },
            });
        }

    }
    chekcremark = (remarkcontent) => {
        let _remarkcontent = remarkcontent;
        _remarkcontent = _remarkcontent.replace(/<p>/ig, '').replace(/<\/p>/ig, '').replace(/ /ig, "").replace(/&nbsp;/ig, "").replace(/<br \/>/ig, "");
        _remarkcontent = _remarkcontent.replace(/\\n/ig, "");
        if (trim(_remarkcontent).length === 0) {
            return false;
        }
        return true;
    }


    getConfirmContent(allowRemind, nodename) {
        const { WeaCheckbox } = getEcCom();
        const { requestWithdrawStore } = this.props;
        const { isRemind } = requestWithdrawStore;
        let text = getLabel(516798, '确定要将流程撤回到“$nodename$”节点吗?');
        text = text.replace('$nodename$', nodename);
        text = text.replace('$nodeName $', nodename);//英文环境下翻译偏差
        if (allowRemind == 1) {
            return (<div style = {{ 'word-wrap': 'break-word','word-break': 'keep-all'}}>{text}<div style={{ marginTop: '20px' }}><span style={{ marginRight: 10, fontSize:'12px' }}>
                <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@pgt9ie`} value={isRemind} onChange={(value) => { requestWithdrawStore.setIsRemind(value); }} />&nbsp;&nbsp;{getLabel(515992, '通知已提交流程的操作者')}</span></div></div>);
        } else {
            return (<div style = {{ 'word-wrap': 'break-word','word-break': 'keep-all'}}>{text}</div>);
        }
    }
}